from django.db import models
from shortuuidfield import ShortUUIDField
from datetime import datetime
import hashlib

# Create your models here.
# 用户表
class UserModel(models.Model):
    GENDER_CHOICES = (
        (u'0', u'正常'),
        (u'1', u'注销'),
    )
    id = ShortUUIDField(primary_key=True, verbose_name="用户id")
    name = models.CharField(null=False, blank=False, max_length=32, verbose_name="用户名")
    phone = models.CharField(unique=False, max_length=11, null=False, blank=False, verbose_name="手机号")
    password = models.CharField(verbose_name="密码", null=False, blank=False, max_length=64)
    register = models.CharField(default=datetime.now().strftime("%Y-%m-%d %H:%M:%S"), max_length=20,
                                verbose_name="注册日期")
    login = models.DateTimeField(auto_now_add=True, verbose_name='最后一次登录时间')
    money = models.CharField(max_length=32, verbose_name="分享佣金")
    status = models.CharField(max_length=2, choices=GENDER_CHOICES, default='0', verbose_name="是否注销")

    def save(self, *args, **kwargs):
        self.password = hashlib.sha256(self.password.encode("utf-8")).hexdigest()
        super(UserModel, self).save(*args, **kwargs)

    class Meta:
        db_table = 'user'
        verbose_name = '用户表'
        verbose_name_plural = verbose_name


# 教师表
class TeacherModel(models.Model):
    GENDER_CHOICES = (
        (u'0', u'正常'),
        (u'1', u'注销'),
    )
    GENDER_CHOICES_LEVEL = (
        (u'0', u'初级'),
        (u'1', u'中级'),
        (u'2', u'高级'),
        (u'3', u'特级'),
    )
    id = ShortUUIDField(primary_key=True, verbose_name="教师id")
    name = models.CharField(null=False, blank=False, max_length=32, verbose_name="用户名")
    phone = models.CharField(unique=False, max_length=11, null=False, blank=False, verbose_name="手机号")
    password = models.CharField(verbose_name="密码", null=False, blank=False, max_length=64)
    register = models.CharField(default=datetime.now().strftime("%Y-%m-%d %H:%M:%S"), max_length=20,
                                verbose_name="注册日期")
    login = models.DateTimeField(auto_now_add=True, verbose_name='最后一次登录时间')
    status = models.CharField(max_length=2, choices=GENDER_CHOICES, default='0', verbose_name="是否注销")
    level = models.CharField(max_length=2, choices=GENDER_CHOICES_LEVEL, default='0', verbose_name="教师等级")

    def save(self, *args, **kwargs):
        self.password = hashlib.sha256(self.password.encode("utf-8")).hexdigest()
        super(TeacherModel, self).save(*args, **kwargs)

    class Meta:
        db_table = 'teacher'
        verbose_name = '教师表'
        verbose_name_plural = verbose_name


# 用户收藏表
class CollectionModel(models.Model):
    id = ShortUUIDField(primary_key=True, verbose_name="收藏表id")
    collection = models.CharField(default=datetime.now().strftime("%Y-%m-%d %H:%M:%S"), max_length=20,
                                  verbose_name="收藏日期")
    project = models.CharField(max_length=32, verbose_name="收藏项目id")
    name = models.CharField(max_length=32, verbose_name="课程名称")
    introduce = models.CharField(max_length=255, verbose_name="课程介绍")
    money = models.CharField(max_length=32, verbose_name="课程费用")
    share_user = models.CharField(max_length=32, verbose_name="分享人")
    number = models.CharField(max_length=255, verbose_name="学习人数")
    apply_start_time = models.DateField(auto_now=False, verbose_name="报名开始时间")
    apply_end_time = models.DateField(auto_now=False, verbose_name="报名结束时间")
    start_time = models.DateField(auto_now=False, verbose_name="开始学习时间")
    end_time = models.DateField(auto_now=False, verbose_name="结束学习时间")
    learning = models.CharField(max_length=255, verbose_name="学习时长")
    phone = models.CharField(max_length=32, verbose_name="用户手机号")



    class Meta:
        db_table = 'collection'
        verbose_name = '收藏表'
        verbose_name_plural = verbose_name


# 订单表
class OrderModel(models.Model):
    id = ShortUUIDField(primary_key=True, verbose_name="订单id")
    number = models.CharField(max_length=255, verbose_name="订单编号")
    order_time = models.CharField(default=datetime.now().strftime("%Y-%m-%d %H:%M:%S"), max_length=20,
                                  verbose_name="下单时间")
    phone = models.CharField(max_length=32, verbose_name="用户手机号")
    share_user = models.CharField(max_length=32, verbose_name="分享人")
    project = models.CharField(max_length=32, verbose_name="下单课程id")
    course_name = models.CharField(max_length=32, verbose_name="课程名称")
    money = models.CharField(max_length=32, verbose_name="课程费用")
    num = models.CharField(max_length=32, verbose_name="课程数量")
    introduce = models.CharField(max_length=255, verbose_name="课程介绍")
    course_number = models.CharField(max_length=255, verbose_name="学习人数")
    apply_start_time = models.DateField(auto_now=False, verbose_name="报名开始时间")
    apply_end_time = models.DateField(auto_now=False, verbose_name="报名结束时间")
    start_time = models.DateField(auto_now=False, verbose_name="开始学习时间")
    end_time = models.DateField(auto_now=False, verbose_name="结束学习时间")
    learning = models.CharField(max_length=255, verbose_name="学习时长")
    class Meta:
        db_table = 'order'
        verbose_name = '订单表'
        verbose_name_plural = verbose_name


# 购物车
class ShoppingCartModel(models.Model):
    id = ShortUUIDField(primary_key=True, verbose_name="购物车id")
    project = models.CharField(max_length=32, verbose_name="项目id")
    name = models.CharField(max_length=32, verbose_name="课程名称")
    num = models.CharField(max_length=32, verbose_name="收藏数量")
    share_user = models.CharField(max_length=32, verbose_name="分享人")
    introduce = models.CharField(max_length=255, verbose_name="课程介绍")
    image = models.CharField(max_length=255, verbose_name="课程图片")
    money = models.CharField(max_length=32, verbose_name="课程费用")
    number = models.CharField(max_length=255, verbose_name="学习人数")
    apply_start_time = models.DateField(auto_now=False, verbose_name="报名开始时间")
    apply_end_time = models.DateField(auto_now=False, verbose_name="报名结束时间")
    start_time = models.DateField(auto_now=False, verbose_name="开始学习时间")
    end_time = models.DateField(auto_now=False, verbose_name="结束学习时间")
    learning = models.CharField(max_length=255, verbose_name="学习时长")
    phone = models.CharField(max_length=32, verbose_name="用户手机号")

    class Meta:
        db_table = 'cart'
        verbose_name = '购物车'
        verbose_name_plural = verbose_name